<?php

namespace app\common\model;

use basic\ModelBasic;

class Recomand extends ModelBasic
{
    //protected $table = 'eb_recomand';
    //const R_TYPE = 1;

	use \traits\ModelTrait;

	/**
	 * 设置where条件
	 * @Author   FW
	 * @DateTime 2021-01-12
	 * @Email    123763712@qq.com
	 * @param    [type]           $where [description]
	 * @param    string           $alias [description]
	 * @param    [type]           $model [description]
	 */
	public static function setWhere($where,$alias='m',$model=null){
        $model = $model===null ? new self() : $model;

        $model->alias($alias);

        if ( isset($where['type']) && $where['type'] > 0 ) {
            $model->where(sprintf('%s.type=%d' , $alias , $where['type']));
        }

        if ( isset($where['status']) && $where['status'] != '' ) {
            $model->where(sprintf('%s.status=%d' , $alias , $where['status']));
        }

        if ( isset($where['title']) && $where['title'] != '' ) {
            $model->where($alias.'.title' , 'LIKE' , '%' . $where['title'] . '%');
        }

        if ( isset($where['order']) && $where['order'] != '' ) {
            $model->order($alias.'.' . $where['order']);
        } else {
            $model->order(sprintf('%s.sort asc,%s.id desc' , $alias,$alias));
        }

        return $model;
    }

    /**
     * 列表
     * @Author   FW
     * @DateTime 2020-09-24
     * @Email    123763712@qq.com
     * @param    [type]
     * @return   [type]
     */
    public static function getList($where){
    	$count = self::setWhere($where)->count();
    	$data  = [];
    	if ( $count == 0 ) {
    		return compact('data','count');
    	}

        $data=self::setWhere($where,'m')
        ->field('m.*')
        ->page($where['page'] ?? 1, $where['limit'] ?? 10)
        ->select();

        if ( !empty($data) ) {
        	$data = $data->toArray();
        	$data = static::process_list($data , $where);
        }

        return compact('data','count');
    }

    /**
     * 处理列表数据
     * @Author   FW
     * @DateTime 2021-01-12
     * @Email    123763712@qq.com
     * @param    array            $list [description]
     * @return   [type]                 [description]
     */
    public static function process_list (array $list)
    {
        foreach ($list as $key => &$item) {
			$item['routes']         = [];
            $item['status_display'] = static::$boolean[$item['status']] ?? $item['status'];
        }
        return $list;
    }

    /**
     * [getOne description]
     * @Author   FW
     * @DateTime 2021-01-12
     * @Email    123763712@qq.com
     * @param    [type]           $id [description]
     * @return   [type]               [description]
     */
    public static function getOne ($id)
    {
        $item = self::alias('m')->where('m.id',$id)
            ->find();

        if ( empty($item) ) {
            return [];
        }
        $item['status_display'] = static::$boolean[$item['status']] ?? $item['status'];
        return $item;
    }
} //Class End
